package com.shine.sys.web.controller;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.shine.common.web.controller.BaseController;
import com.shine.common.web.model.DataGrid;
import com.shine.common.web.model.DataTables;
import com.shine.common.web.validate.AjaxResponse;
import com.shine.sys.entity.User;
import com.shine.sys.service.UserService;
import com.shine.sys.web.dto.UserDto;

@Controller
@RequestMapping("/admin/sys/user")
public class UserController extends BaseController {

	@Autowired
	private UserService userService ;
	
	@RequestMapping(value="/main", method=RequestMethod.GET)
	public String main(Model model) {
		return viewName("main") ;
	}
	
	@RequestMapping(value="/main_test", method=RequestMethod.GET)
	public String main_test(Model model) {
		return viewName("main_test") ;
	}
	
	@RequestMapping(value="/form", method=RequestMethod.GET)
	public String form(Integer id, Model model) {
		if(null != id) {
			model.addAttribute("id", id) ;
		}
		return viewName("form") ;
	}
	
	@RequestMapping(value="/add", method=RequestMethod.POST)
	@ResponseBody
	public AjaxResponse add(UserDto dto) {
		this.userService.add(dto) ;
		return new AjaxResponse() ;
	}
	
	
	@RequestMapping(value="/delete/{id}", method=RequestMethod.GET)
	@ResponseBody
	public AjaxResponse delete(@PathVariable String id) {
		this.userService.delete(true, id) ;
		return new AjaxResponse() ;
	}
	
	@RequestMapping(value="/delete", method=RequestMethod.POST)
	@ResponseBody
	public AjaxResponse deleteBatch(@RequestParam String ids, @RequestParam Boolean type) {
		this.userService.delete(type, ids) ;
		return new AjaxResponse() ;
	}
	
	@RequestMapping(value="/deleteForPhysical", method=RequestMethod.POST)
	@ResponseBody
	public AjaxResponse deleteForPhysical(@RequestParam String ids) {
		this.userService.deleteForPhysical(ids) ;
		return new AjaxResponse() ;
	}
	
	@RequestMapping(value="/update", method=RequestMethod.POST)
	@ResponseBody
	public AjaxResponse update(UserDto dto) {
		this.userService.update(dto) ;
		return new AjaxResponse() ;
	}
	
	@RequestMapping(value="/get/{id}", method=RequestMethod.GET)
	@ResponseBody
	public User get(@PathVariable Integer id, Model model) {
		return this.userService.get(id) ;
	}
	
	@RequestMapping(value="/datagrid")
	@ResponseBody
	public DataGrid datagrid(UserDto dto) {
		return this.userService.datagrid(dto) ;
	}
	
	@RequestMapping(value="/datatables")
	@ResponseBody
	public DataTables datatables(UserDto dto) {
		return this.userService.datatables(dto) ;
	}
	
}
